/*
 * Created on 28/01/2013 - 2:24 PM
 */
package sembr.vtk.animation;

import sembr.vtk.visualizing.VtkMultRenderPanel;
import vtk.*;

/**
 *
 * @author adriano
 */
public class Animator 
{
    private String path; 
    private int nrOfFrames;
    private int startTime;
    private int timeStep;
    private vtkPNGReader reader; 
    private vtkDataSetMapper dataSetMapper;
    private vtkScalarBarActor barActor;
    private vtkActor actor;
    private VtkMultRenderPanel panel;
    private vtkRenderer render1;
    private vtkRenderWindow renderWin;
    private vtkRenderWindowInteractor iren;
    private static int counter = 0;
    
    public Animator(String path, int nrOfFrames, int startTime, int timeStep)
    {
       LoadVTKLibraries();
       setPath(path);
       setNrOfFrames(nrOfFrames); 
       setStartTime(startTime);
       setTimeStep(timeStep);
       setReader(new vtkPNGReader());
       setDataSetMapper(new vtkDataSetMapper());
       setPanel(new VtkMultRenderPanel());
       //setBarLegend(new vtkScalarBarActor());
    }
        
    /**
     * @return the path
     */
    public String getPath() 
    {
        return path;
    }
     
    /**
     * @return the nrOfFrames
     */
    public int getNrOfFrames() 
    {
        return nrOfFrames;
    }
    
    /**
     * @return the startTime
     */
    public int getStartTime() 
    {
        return startTime;
    }  
    
    /**
     * @return the timeStep
     */
    public int getTimeStep() 
    {
        return timeStep;
    }
    
    /**
     * @return the reader
     */
    public vtkPNGReader getReader() 
    {
        return reader;
    }
    
    /**
     * @return the dataSetMapper
     */
    public vtkDataSetMapper getDataSetMapper() 
    {
        return dataSetMapper;
    }
         
    /**
     * @return the actorFieldCollection
     */
    public vtkActor getActor() 
    {
        return actor;
    }
    
       
    /**
     * @return the barActor
     */
    public vtkScalarBarActor getBarActor()
    {
        return barActor;
    }
    
    /**
     * @return the panel
     */
    public VtkMultRenderPanel getPanel() 
    {
        return panel;
    }
        
    /**
     * @param path the path to set
     */
    private void setPath(String path) 
    {
        this.path = path;
    }
      
    /**
     * @param nrOfFrames the nrOfFrames to set
     */
    private void setNrOfFrames(int nrOfFrames) 
    {
        this.nrOfFrames = nrOfFrames;
    }
        
    /**
     * @param startTime the startTime to set
     */
    private void setStartTime(int startTime) 
    {
        this.startTime = startTime;
    }
    
    /**
     * @param timeStep the timeStep to set
     */
    private void setTimeStep(int timeStep) 
    {
        this.timeStep = timeStep;
    }
        
    /**
     * @param reader the reader to set
     */
    private void setReader(vtkPNGReader reader) 
    {
        this.reader = reader;
    }
            
    /**
     * @param dataSetMapper the dataSetMapper to set
     */
    private void setDataSetMapper(vtkDataSetMapper dataSetMapper) 
    {
        this.dataSetMapper = dataSetMapper;
    }
    
    /**
     * @param canvas the canvas to set
     */
    private void setPanel(VtkMultRenderPanel panel) 
    {
        this.panel = panel;
    }
    
    private void setBarLegend(vtkScalarBarActor barActor)
    {   
        this.barActor = barActor;
        this.barActor.SetLookupTable(getDataSetMapper().GetLookupTable());
        this.barActor.GetTitleTextProperty().BoldOff();
        this.barActor.SetTitle("ez");        
        this.barActor.SetNumberOfLabels(6);
        this.barActor.SetWidth(0.06);
        this.barActor.SetHeight(0.83);    
    }
    
    /**
     * @param actorFieldCollection the actorFieldCollection to set
     */
    private void setActor(vtkActor actor) 
    {
        this.actor = actor;
    }
     
    public void createScene()
    {
        String prefix = getPath() + "sembr.dxf.presentation/simulation/ez-000000.00";
        // reader settings
        setReader(new vtkPNGReader());
        getReader().SetFilePrefix(prefix);           
        getReader().SetDataByteOrderToBigEndian();
        //getReaderCollection()[i].SetFileName(prefix + ".t" + String.valueOf(getStartTime() + (i * getTimeStep())) + ".png");
        getReader().SetDataExtent(0, 419, 0, 179, 0, 0);
        getReader().SetFilePattern("%s" + ".t%04d.png");
        //getReader().Update();

        // mapper settings for reader output dataset
        setDataSetMapper(new vtkDataSetMapper());
        getDataSetMapper().SetInput(getReader().GetOutput());
        //getDataSetMapper().Update();

        setActor(new vtkActor());
        getActor().SetMapper(getDataSetMapper());  
        getPanel().GetRenderer().AddActor(getActor());   
//        
//        getPanel().getIren().Initialize();        
//        getPanel().GetRenderer().AddObserver("StartEvent", this, "startRender");
//        getPanel().getIren().CreateRepeatingTimer(getNrOfFrames());
//        getPanel().getIren().Start();
        
        render1 = new vtkRenderer();
        render1.SetBackground(0.1, 0.2, 0.4);
        render1.AddActor(actor);
        
        renderWin = new vtkRenderWindow();
        renderWin.AddRenderer(render1);
        renderWin.SetSize(600, 600);
        
        iren = new vtkRenderWindowInteractor();
        iren.SetRenderWindow(renderWin);
        
        iren.Initialize();
        render1.AddObserver("StartEvent", this, "startRender");
        iren.CreateRepeatingTimer(getNrOfFrames());
        iren.Start();
    }
    
    /**
     * startRender method will create the network beetween vtk data objects and process 
     */   
    private void startRender() 
    { 
        String prefix = getPath() + "sembr.dxf.presentation/simulation/ez-000000.00";
        counter++;
        System.out.println(counter);
        // reader settings
        setReader(new vtkPNGReader());
        getReader().SetFilePrefix(prefix);           
        getReader().SetDataByteOrderToBigEndian();
        //getReaderCollection()[i].SetFileName(prefix + ".t" + String.valueOf(getStartTime() + (i * getTimeStep())) + ".png");
        getReader().SetDataExtent(0, 419, 0, 179, counter, counter);
        getReader().SetFilePattern("%s" + ".t%04d.png");
        getReader().Modified();         
    }
    
    public static void main(String[] args)
    {
        String path = "/home/adriano/Projects/SEM-BR/trunk/";
        Animator anime = new Animator(path, 6000, 0, 1);
        anime.createScene();
    }
       
    private static void LoadVTKLibraries()
    {
        System.loadLibrary("vtkCommon");
        System.loadLibrary("vtkFiltering");
        System.loadLibrary("vtkGenericFiltering");
        System.loadLibrary("vtkGraphics");
        System.loadLibrary("vtkImaging");
        System.loadLibrary("vtksys");
        System.loadLibrary("vtkexpat");
        System.loadLibrary("vtkfreetype");
        System.loadLibrary("vtkftgl");
        System.loadLibrary("vtkzlib");
        System.loadLibrary("vtktiff");
        System.loadLibrary("vtkpng");
        System.loadLibrary("vtkIO");
        System.loadLibrary("vtkRendering");
        System.loadLibrary("vtkVolumeRendering");
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkGenericFilteringJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkHybridJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
    }   
}
